package com.decompiler.ui;

import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
import java.awt.*;

/**
 * 现代化滚动面板，具有圆角滚动条
 */
public class ModernScrollPane extends JScrollPane {
    private Color trackColor;
    private Color thumbColor;
    private Color thumbHoverColor;
    
    /**
     * 创建一个现代化滚动面板
     * 
     * @param view 视图组件
     * @param trackColor 滚动条轨道颜色
     * @param thumbColor 滚动条滑块颜色
     * @param thumbHoverColor 滚动条滑块悬停颜色
     */
    public ModernScrollPane(Component view, Color trackColor, Color thumbColor, Color thumbHoverColor) {
        super(view);
        this.trackColor = trackColor;
        this.thumbColor = thumbColor;
        this.thumbHoverColor = thumbHoverColor;
        
        setupScrollBars();
    }
    
    /**
     * 创建一个使用默认颜色的现代化滚动面板
     * 
     * @param view 视图组件
     */
    public ModernScrollPane(Component view) {
        this(view, new Color(240, 240, 240), new Color(180, 180, 180), new Color(140, 140, 140));
    }
    
    private void setupScrollBars() {
        // 设置垂直滚动条
        JScrollBar verticalScrollBar = getVerticalScrollBar();
        verticalScrollBar.setUI(new ModernScrollBarUI(trackColor, thumbColor, thumbHoverColor));
        verticalScrollBar.setUnitIncrement(16); // 增加滚动速度
        
        // 设置水平滚动条
        JScrollBar horizontalScrollBar = getHorizontalScrollBar();
        horizontalScrollBar.setUI(new ModernScrollBarUI(trackColor, thumbColor, thumbHoverColor));
        horizontalScrollBar.setUnitIncrement(16); // 增加滚动速度
        
        // 移除边框
        setBorder(BorderFactory.createEmptyBorder());
        
        // 设置滚动条始终显示
        setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    }
    
    /**
     * 现代化滚动条UI
     */
    private static class ModernScrollBarUI extends BasicScrollBarUI {
        private final Color trackColor;
        private final Color thumbColor;
        private final Color thumbHoverColor;
        private final int THUMB_SIZE = 8;
        
        public ModernScrollBarUI(Color trackColor, Color thumbColor, Color thumbHoverColor) {
            this.trackColor = trackColor;
            this.thumbColor = thumbColor;
            this.thumbHoverColor = thumbHoverColor;
        }
        
        @Override
        protected void configureScrollBarColors() {
            // 不使用父类的颜色配置
        }
        
        @Override
        protected JButton createDecreaseButton(int orientation) {
            return createEmptyButton();
        }
        
        @Override
        protected JButton createIncreaseButton(int orientation) {
            return createEmptyButton();
        }
        
        private JButton createEmptyButton() {
            JButton button = new JButton();
            button.setPreferredSize(new Dimension(0, 0));
            button.setMinimumSize(new Dimension(0, 0));
            button.setMaximumSize(new Dimension(0, 0));
            return button;
        }
        
        @Override
        protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(trackColor);
            g2d.fillRect(trackBounds.x, trackBounds.y, trackBounds.width, trackBounds.height);
            g2d.dispose();
        }
        
        @Override
        protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) {
            if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) {
                return;
            }
            
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            
            // 根据鼠标状态选择颜色
            if (isDragging) {
                g2d.setColor(thumbHoverColor);
            } else if (isThumbRollover()) {
                g2d.setColor(thumbHoverColor);
            } else {
                g2d.setColor(thumbColor);
            }
            
            // 绘制圆角滑块
            int x = thumbBounds.x;
            int y = thumbBounds.y;
            int width = thumbBounds.width;
            int height = thumbBounds.height;
            
            if (scrollbar.getOrientation() == JScrollBar.VERTICAL) {
                // 垂直滚动条，居中显示滑块
                x = thumbBounds.x + (thumbBounds.width - THUMB_SIZE) / 2;
                width = THUMB_SIZE;
            } else {
                // 水平滚动条，居中显示滑块
                y = thumbBounds.y + (thumbBounds.height - THUMB_SIZE) / 2;
                height = THUMB_SIZE;
            }
            
            g2d.fillRoundRect(x, y, width, height, THUMB_SIZE, THUMB_SIZE);
            g2d.dispose();
        }
        
        @Override
        protected void setThumbBounds(int x, int y, int width, int height) {
            super.setThumbBounds(x, y, width, height);
            scrollbar.repaint();
        }
    }
}